import request from '@/utils/request';
import { RequestOptionsInit } from 'umi-request';

const get = <URL extends keyof API>(
  url: URL,
  params?: API[URL]['params'],
  options?: RequestOptionsInit,
): Promise<API[URL]['result']> => {
  let op: RequestOptionsInit = {};
  if (options) op = Object.assign(op, params, options);

  return request(url, options ? op : { params: params ? params : {} });
};

const getRest = (url: any) => {
  return request(url);
};

const post = <URL extends keyof API>(
  url: URL,
  data?: API[URL]['params'],
  options?: RequestOptionsInit,
): Promise<API[URL]['result']> => {
  let op: RequestOptionsInit = {};
  if (options) op = Object.assign(op, data, options);

  return request(
    url,
    options
      ? op
      : {
          data: data ? data : {},
          method: 'post',
        },
  );
};

const put = <URL extends keyof API>(
  url: URL,
  data?: API[URL]['params'],
  options?: RequestOptionsInit,
): Promise<API[URL]['result']> => {
  let op: RequestOptionsInit = {};
  if (options) op = Object.assign(op, data, options);

  return request(
    url,
    options
      ? op
      : {
          data: data ? data : {},
          method: 'put',
        },
  );
};

const del = <URL extends keyof API>(
  url: URL,
  data?: API[URL]['params'],
  options?: RequestOptionsInit,
): Promise<API[URL]['result']> => {
  let op: RequestOptionsInit = {};
  if (options) op = Object.assign(op, data, options);

  return request(
    url,
    options
      ? op
      : {
          data: data ? data : {},
          method: 'delete',
        },
  );
};

export { get, post, put, del,getRest };
